﻿namespace RSSAggregator_WebRole
{
    using System.Linq;
    using Microsoft.Samples.ServiceHosting.StorageClient;

    public class RSSAggregatorContext : TableStorageDataServiceContext
    {
        //public IQueryable<User> Users2
        //{
        //    get
        //    {
        //        return this.CreateQuery<User>("Users2");
        //    }
        //}

        public IQueryable<Feed> Feeds
        {
            get
            {
                return this.CreateQuery<Feed>("feeds");
            }
        }

        public IQueryable<RssLink> Links
        {
            get
            {
                return this.CreateQuery<RssLink>("links");
            }
        }

        //public IQueryable<FeedItem> FeedItems2
        //{
        //    get
        //    {
        //        return this.CreateQuery<FeedItem>("FeedItems2");
        //    }
        //}

        public RSSAggregatorContext(StorageAccountInfo accountInfo)
            : base(accountInfo)
        {

        }

        public void AddFeed(Feed f)
        {
            this.AddObject("feeds", f);
            foreach (RssLink r in f.GetUrls())
            {
                this.AddLink(r);
            }
            //this.SaveChanges();
        }

        public void RemoveFeed(string feedName)
        {
            var feed = from f in Feeds where f.Title.Equals(feedName) select f;
            this.DeleteObject(feedName);
            this.SaveChanges();
        }


        public void AddLink(RssLink rssLink)
        {
            this.AddObject("links", rssLink);
            //this.SaveChanges();
        }

    }
}
